package org.lovely.base.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.lovely.base.domain.constant.BaseConstant;
import org.lovely.base.domain.entity.BaseData;
import org.lovely.base.mapper.BaseDataMapper;
import org.lovely.base.service.BaseDataService;
import org.lovely.common.base.dto.QueryBaseDataDTO;
import org.lovely.common.base.vo.BaseDataVo;
import org.lovely.common.dto.BasePage;
import org.lovely.common.dto.Result;
import org.lovely.websocket.DeerWebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


/**
 * <p>
 * description:
 * </p>
 *
 * @author kaitong.wang@hand-china.com 2021/3/30 12:44 星期二
 */
@Service
@Slf4j
public class BaseDataServiceImpl implements BaseDataService {

    @Autowired
    private BaseDataMapper baseDataMapper;

    @Autowired
    private DeerWebSocketServer webSocketServer;

    @Override
    @DS(BaseConstant.DataBaseName.SLAVE_NAME)
    public Result<IPage<BaseData>> getList(QueryBaseDataDTO query) {
        IPage<BaseData> baseDataIPage = new Page<>(query.getPage(), query.getLimit());

        QueryWrapper<BaseData> queryWrapper = new QueryWrapper<BaseData>();
        if(!StringUtils.isEmpty(query.getName())){
            queryWrapper.like("name", "%"+query.getName() + "%");
        }
        IPage<BaseData> baseDataIPage1 = baseDataMapper.selectPage(baseDataIPage, queryWrapper);
//        log.info("查询出的数据为：{},", JSONObject.toJSON(baseDataIPage1));
        return  Result.returnSuccess(baseDataIPage1);
    }

    @Override
    @DS(BaseConstant.DataBaseName.MASTER_NAME)
    @Transactional(rollbackFor = Exception.class)
    public Result add(BaseData baseData) {
//        log.info("新增基础数据");
        baseDataMapper.insert(baseData);
//        int i =1/0;
        return Result.returnSuccess();
    }
}
